package com.yanze.schoolinformation.viewModule.relax;

import android.text.TextUtils;

import com.yanze.schoolinformation.app.C;
import com.yanze.schoolinformation.model.bean.DuanZiBean;
import com.yanze.schoolinformation.model.bean.QsbkListBean;
import com.yanze.schoolinformation.model.http.DeforObserver;
import com.yanze.schoolinformation.model.http.reqo.DataReqo;
import com.yanze.schoolinformation.utils.ToastUtils;
import com.yanze.schoolinformation.view.information.relax.JokeFragment;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Function;

/**
 * Created by MrZheng on 2018/4/28.
 */

public class JokeViewModel {
    private JokeFragment mFragment;
    private JokeNavigator mNavigator;
    private int page = 1;

    public JokeViewModel(JokeFragment mFragment) {
        this.mFragment = mFragment;
    }

    public void setNavigator(JokeNavigator mNavigator) {
        this.mNavigator = mNavigator;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public void loadDuanziData() {
        Function<QsbkListBean, Observable<List<DuanZiBean>>> function = new Function<QsbkListBean, Observable<List<DuanZiBean>>>() {
            @Override
            public Observable<List<DuanZiBean>> apply(QsbkListBean bean) {
                List<DuanZiBean> lists = new ArrayList<>();
                if (bean != null && bean.getItems() != null && bean.getItems().size() > 0) {
                    List<QsbkListBean.ItemsBean> items = bean.getItems();
                    for (QsbkListBean.ItemsBean bean1 : items) {
                        DuanZiBean duanZiBean = new DuanZiBean();
                        duanZiBean.setContent(bean1.getContent());
                        duanZiBean.setCreateTime(bean1.getPublished_at());
                        QsbkListBean.ItemsBean.TopicBean topic = bean1.getTopic();
                        QsbkListBean.ItemsBean.UserBean user = bean1.getUser();
                        if (topic != null) {
                            duanZiBean.setCategoryName(topic.getContent());
                        }
                        if (user != null) {
                            duanZiBean.setName(user.getLogin());
                            String thumb = user.getThumb();
                            if (!TextUtils.isEmpty(thumb)) {
                                StringBuffer stringBuffer = new StringBuffer();
//                                String s = thumb.replaceAll("\\\\\\\\", "\\");
                                stringBuffer.append("http:");
                                stringBuffer.append(thumb);
                                duanZiBean.setAvatarUrl(stringBuffer.toString());
                            }
                        }
//                        bean1.getHot_comment();
                        lists.add(duanZiBean);
                    }
                }
                return Observable.just(lists);
            }
        };

        DataReqo.getInstant().getQsbkList(C.url.API_QSBK, page)
                .flatMap(function)
                .subscribe(new DeforObserver<List<DuanZiBean>>() {
                    @Override
                    public void _onSubscribe(Disposable disposable) {
                        mFragment.addDisposable(disposable);
                    }

                    @Override
                    public void _onNext(List<DuanZiBean> entity) {
                        mNavigator.showLoadSuccessView();
                        if (page == 1) {
                            if (entity != null
                                    && entity.size() > 0) {
                                mNavigator.showAdapterView(entity);
                            }else{
                                mNavigator.loadListFailure();
                            }
                        }else{
                            if (entity != null
                                    && entity.size() > 0) {
                                mNavigator.refreshAdapter(entity);
                            }else{
                                mNavigator.showListNoMoreLoading();
                            }
                        }
                    }

                    @Override
                    public void _onError(String msg) {
                        ToastUtils.show(msg);
                        if (page > 1) {
                            page--;
                        }
                        mNavigator.loadListFailure();
                    }
                });
    }

    public void onDestory() {
        mFragment = null;
        mNavigator = null;
    }
}
